<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Mail;
use DB;
use Crypt;
class ResetController extends Controller
{
    public function forget()
    {
    	return view('admin.reset.forget');
    }

    //发送邮件
    public function send(Request $request)
    {
    	$mail=$request->email;
    	$com=trim(strrchr($mail,'@'),'@');
    	$email='mail.'.$com;
    	
    	$res=DB::table('manager')->where('email',$request->email)->first();
    	if(!$res)
    	{
    		return back()->with(['info'=>'邮箱不存在']);
    	}

    	$s=Mail::send('admin.reset.email', ['user'=>$res], function ($m) use ($res){

            $m->to($res->email)->subject('找回密码!');
     });
    	if($s)
    	{
    		return view('admin.reset.send',['mail'=>$email]);	
    	}else
    	{
    		return back()->with(['info'=>'邮箱不存在']);
    	}


    }

    public function inputpass($id,$token)
    {
    	$t=DB::table('manager')->where('id',$id)->first()->token;
    	if($token!=$t)
    	{
    		return redirect('admin.reset.forget')->with(['info'=>'源地址不合法']);
    	}else
    	{
    		return redirect('admin/reset/newpass')->with(['info'=>'请输入新密码','id'=>$id]);
    	}
    }

    public function newpass()
    {

    	$id=session('id');
    	return view('admin.reset.newpass',['id'=>$id]);
    }
    public function reset(Request $request)
    {

        
    	if($request->password!=$request->repassword)
    	{
    		return back()->with(['info'=>'两次密码不同']);
    	}
    	// dd($request->all());
    	$res=DB::table('manager')->where('id',$request->id)->update(['password'=>Crypt::encrypt($request->password)]);
    	if($res)
    	{
    		return redirect('admin/login')->with(['info'=>'修改成功']);
    	}else
    	{
    		return redirect('admin/login')->with(['info'=>'修改成功']);
    	}
    	

    }





}
